<?
include "libhtml.php";
include "libsession.php";
requireLogin();
echoHeader("Workflow Templates");
?>
<h1>Workflow Templates</h1>
<br><br>
<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">

  <tbody>
    <tr>
      <th>Workflow Template Name<br></th>
      <th>Used by # Documents<br></th>
      <th>Actions</th>
    </tr>
    <? 
    	// find workflow templates
		$sql = "SELECT Id, Name, 
					(SELECT COUNT(1) FROM Document WHERE WorkflowTemplateId=wft.Id) AS Occurs
				FROM WorkflowTemplate wft";
		$result = query_db($sql); 
		
    	while ($template = mysql_fetch_array($result, MYSQL_ASSOC))
    	{
	    	$url = "<a href='WorkflowTemplate_Edit.php?templateid=" . $template["Id"] . "'>";
	    	echo "<tr>";
	    	echo "<td style='vertical-align: top;'>" . $url . $template["Name"] . "</a><br></td>";
	    	echo "<td style='vertical-align: top;'>" . $template["Occurs"] . "<br></td>";
	    	
	    	$templateid = $template["Id"];
	    	
	    		echo "<td>";
		    	echo "	<a href='WorkflowTemplate_Edit.php?templateid=$templateid'>";
		    	echo "		<img src='images/green-checkmark.gif' height='30' width='30' border='0' alt='Update Template'>";
		    	echo "	</a>";
				
	    	
	    	if ($template["Occurs"] == 0)
	    	{
		    	// give users the delete button only if it's not in use
		    	echo "	<form action=WorkflowUpdate.php style='display: inline;' method=post>";
				echo "		<input type=hidden name='action' value='deletetemplate'>";
				echo "		<input type=hidden name=targetid value=$templateid>";
				echo "		<input type=hidden name=redirecturl value='WorkflowTemplate_List.php'>";
				echo "		<input type=image SRC='images/red-x.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Delete Template'>";
				echo "	</form>";	
	    	}
	    	
	    	echo "	</td>";
	    	echo "</tr>";
    	}
    	mysql_free_result($result);
    ?>
  </tbody>
</table>

<div style='text-align: center;'>
	<br>
	<form action='WorkflowUpdate.php' method=post>
		<input type=hidden name=action value='addtemplate'>
		<input type=hidden name=redirecturl value='WorkflowTemplate_List.php'>
		<input type=image SRC='images/blue-plus.jpg' HEIGHT='30' WIDTH='30' BORDER='0' ALT='Add Template'>
	</form>
</div>

<? echoFooter() ?>